<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page
	import="com.fourtwoeight.controllers.WebConstants,
	com.fourtwoeight.controllers.FileWrapper,
	java.util.*,
	java.io.*"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>


<%
	String currentDir = (String)request.getSession().getAttribute(WebConstants.CURRENT_DIRECTORY);
	String imagesDirectoryURL = (String)request.getSession().getAttribute(WebConstants.IMAGE_DIRECTORY_URL);
	Collection<FileWrapper> fileList = (Collection<FileWrapper>)request.getAttribute(WebConstants.CURRENT_DIRECTORY_LIST);
	
	List<String> listOfImageExtensions = new ArrayList<String>();
	listOfImageExtensions.add("JPG");
	listOfImageExtensions.add("jpg");
	listOfImageExtensions.add("jpeg");
	listOfImageExtensions.add("gif");
	listOfImageExtensions.add("GIF");
	listOfImageExtensions.add("PNG");
	listOfImageExtensions.add("png");

	List<String> listOfMovieExtensions = new ArrayList<String>();
	listOfMovieExtensions.add("AVI");
	listOfMovieExtensions.add("avi");
	listOfMovieExtensions.add("mpg");
	listOfMovieExtensions.add("MPG");
	listOfMovieExtensions.add("mpeg");
%>

<%! 

public String iterateCurrentDirectory(String directoryPath, HttpServletRequest request) {

	StringBuffer links = new StringBuffer();
	String cumulativePath = "";
	//links.append(addLink("ROOT", "/"));
	String[] directories = directoryPath.split("/");
	
	for(String directory:directories) {
		cumulativePath = cumulativePath + directory;
		if(!cumulativePath.endsWith("/")) {
			cumulativePath = cumulativePath + "/";	
		}
		links.append(addLink(directory, request.getContextPath() +
							"/thumbs.form?" +
							"currentDirectory=" + cumulativePath));	
	}
	
	return links.toString();
}

public String addLink(String name, String link) {
	return "<a href=\"" + link + "\">" + name + "</a> > \n";
}
public String iterateWrappedFiles(Collection<FileWrapper> files, 
								HttpServletRequest request, 
								List<String> listOfImageExtensions, 
								String imagesDirectoryURL) {
	StringBuffer buff = new StringBuffer();
	
	Iterator<FileWrapper> iter = files.iterator();
	
	while(iter.hasNext()) {
		FileWrapper file = iter.next();
		buff.append(renderThumbnail(file, request, listOfImageExtensions, imagesDirectoryURL));		
	}
	
	return buff.toString();
}

public String getIconFile(File file, List<String> listOfImageExtensions, 
							HttpServletRequest request, 
							String imagesDirectoryURL, String currentDir) {
	String defaultIcon = "none.jpg";
	String path = file.getPath();
	if(path.indexOf(".") > 1 && path.length() >= 3) {
		String extension = path.substring(path.length() - 3, path.length());
	
		if(listOfImageExtensions.contains(extension)) {
		
			return request.getContextPath() +
				"/" + 
				imagesDirectoryURL + 
				currentDir + 
				"/_thumb/thumb_" +
				file.getName();
		}
	}
	
	return defaultIcon;
}

public String renderThumbnail(FileWrapper wrappedFile, 
								HttpServletRequest request, 
								List<String> listOfImageExtensions, 
								String imagesDirectoryURL) {

	String currentDir = (String) request.getSession().getAttribute(WebConstants.CURRENT_DIRECTORY);
	
	StringBuffer buff = new StringBuffer();
	
	File actualFile = wrappedFile.getActualFileToDisplay();
	File previousFile = wrappedFile.getPreviousFile();
	File nextFile = wrappedFile.getNextFile();
	
	buff.append("\n<div class=\"Photo\" align=\"center\">");
	buff.append("\n\t<div class=\"imgBorder\" align=\"center\">");
	
	if(actualFile.isDirectory()) {
		buff.append("\n\t\t<a class=\"Photo\" " +
							"href=\"" +
							request.getContextPath() +
							"/thumbs.form?" +
							"currentDirectory=" + currentDir + "/" + actualFile.getName() +
							"\">\n\t\t\t<img border=\"0\" src=\"" + 
							request.getContextPath() +
							"/images/folder.gif\" height=\"94\" width=\"94\"> \n\t\t</a>\n");
	} else {
		buff.append("\n\t\t<a class=\"Photo\" " +
							"href=\"" +
							request.getContextPath() +
							"/image.form?" + 
							"file=" + currentDir + "/" + actualFile.getName() );
							
		if(previousFile != null && !previousFile.isDirectory()) {
			buff.append("&p=" + currentDir + "/" + previousFile.getName()); 
		}
		
		if(nextFile != null && !nextFile.isDirectory()) {
			buff.append("&n=" + currentDir + "/" + nextFile.getName()); 
		}	
						
		buff.append("\">\n\t\t\t<img border=\"0\" src=\"" +
							getIconFile(actualFile,listOfImageExtensions, request, 
								imagesDirectoryURL,currentDir) +
							"\"/>\n\t\t</a>\n");
	
	}
	buff.append("\t</div>\n");
	buff.append("\n\t<a class=\"Photo\" " +
							"href=\"" +
							request.getContextPath() +
							"/thumbs.form?currentDirectory=" +
							currentDir +
							"/" +
							wrappedFile.getActualFileToDisplay().getName() +
							"\">" +
							wrappedFile.getActualFileToDisplay().getName() +
							"\n\t</a>\n");
	buff.append("</div>\n\n");

	return buff.toString();
} 

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>



	<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
	<title>428 WILSON.COM - Image Library - <%=currentDir%>
	</title>

	<link href="<%=request.getContextPath()%>/images/styles.css"
		rel="stylesheet">
	<script language="javascript"
		src="<%=request.getContextPath()%>/images/x.js"></script>
	<script language="javascript">
function saveCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000))
		var expires = "; expires="+date.toGMTString()
	}
	else expires = ""
	document.cookie = name+"="+value+expires+"; path=/"
}
function setSlideShowCookie() {
	saveCookie('slideShow', 'true');
}
function init() {
	saveCookie('slideShow', '', -1);
	resizeHandler();
}
function resizeHandler() {
	xHeight('divActivity', parseInt(xClientHeight(),10) + parseInt(xScrollTop(),10));
}
window.onresize = resizeHandler
window.onscroll = resizeHandler
</script>
</head>
<body onload="init();" id="index">
	<script language="javascript">
if (navigator.userAgent.indexOf('Opera')!=-1)
	document.write('<style>\ndiv.Photo {\ndisplay: inline-block;\nfloat: none;\n}\n</style>');
</script>
	<div id="divMainHeader">
		<%= iterateCurrentDirectory(currentDir, request)%>
	</div>
	<div id="divMain">

		<%= iterateWrappedFiles(fileList, request, listOfImageExtensions, 
							imagesDirectoryURL) %>

	</div>
	<div style="height: 614px;" id="divActivity">
		<div id="divImageActivity">
			<table border="0" cellpadding="0" cellspacing="0" width="185">
				<tbody>
					<tr>
						<td colspan="3" class="ImagePaneHeader" width="185">
							Image activities
						</td>
					</tr>

					<tr>
						<td class="ImagePaneFrame" width="1">
							<img src="<%=request.getContextPath()%>/images/1x1.gif"
								height="1" width="1">
						</td>
						<td class="ImagePaneRow" width="183">
							<a href="<%=request.getContextPath()%>/DisplayThumbnails.do"
								class="ImagePaneRow"> <img
									src="<%=request.getContextPath()%>/images/FolderUp.gif"
									style="margin-right: 5px;" border="0">Image Library Home</a>
						</td>
						<td class="ImagePaneFrame" width="1">
							<img src="<%=request.getContextPath()%>/images/1x1.gif"
								height="1" width="1">
						</td>
					</tr>

					<tr>
						<td colspan="3" class="ImagePaneFrame" width="185">
							<img src="<%=request.getContextPath()%>/images/1x1.gif"
								height="1" width="1">
						</td>
					</tr>
				</tbody>
			</table>
		</div>
		<div id="divInfoActivity">
			<table border="0" cellpadding="0" cellspacing="0" width="185">
				<tbody>
					<tr>
						<td colspan="3" class="InfoPaneHeader" width="185">
							Information
						</td>
					</tr>
					<tr>
						<td class="InfoPaneFrame" width="1">
							<img src="<%=request.getContextPath()%>/images/1x1.gif"
								height="1" width="1">
						</td>
						<td class="InfoPaneRow" width="183"></td>
						<td class="InfoPaneFrame" width="1">
							<img src="<%=request.getContextPath()%>/images/1x1.gif"
								height="1" width="1">
						</td>
					</tr>
					<tr>
						<td colspan="3" class="InfoPaneFrame" width="185">
							<img src="<%=request.getContextPath()%>/images/1x1.gif"
								height="1" width="1">
						</td>
					</tr>
				</tbody>
			</table>
		</div>

	</div>
</body>

</html:html>
